package com.qincji.webviewbrowser;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.global.resource.Resource;

/**
 * 描述：顶部栏（topbar）简单封装
 * 作者：qincji on 2021/6/23 15:59
 * 起于：https://zqxq.club
 * 邮箱：xhunmon@126.com
 */
public class TopBar {

    private Text titleTxt;
    private DirectionalLayout topBar;

    public TopBar(AbilitySlice component, Component.ClickedListener leftListener) {
        this(component, null, leftListener, 0, null, null);
    }

    public TopBar(AbilitySlice component, String title, Component.ClickedListener leftListener) {
        this(component, title, leftListener, 0, null, null);
    }

    public TopBar(AbilitySlice component, String title, Component.ClickedListener leftListener, int rightImgXml, String right, Component.ClickedListener rightListener) {
//        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_top_bar, null, false);
        component.findComponentById(ResourceTable.Id_tob_bar_left).setClickedListener(leftListener);
        topBar = (DirectionalLayout) component.findComponentById(ResourceTable.Id_top_bar);
        Button rightBtn = (Button) component.findComponentById(ResourceTable.Id_tob_bar_right);
        if (right != null) {
            rightBtn.setText(right);
        }
        if (rightImgXml > 0) {
            Resource resource = null;
            try {
                resource = component.getResourceManager().getResource(rightImgXml);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resource != null){
                rightBtn.setAroundElements(new PixelMapElement(resource), null, null, null);
            }
        }
        if (rightListener != null) {
            rightBtn.setClickedListener(rightListener);
        }
        titleTxt = (Text) component.findComponentById(ResourceTable.Id_tob_bar_title);
        if (title != null) {
            titleTxt.setText(title);
        }
    }

    public void setTitle(String title) {
        titleTxt.setText(title);
    }

    public void hid(){
        topBar.setVisibility(Component.HIDE);
    }

    public void show(){
        topBar.setVisibility(Component.VISIBLE);
    }
}